package com.javalong.affairs.module.renwujinzhang.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.ToolbarActivity;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.module.renwujinzhang.ui.ImageEditView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectFileView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by 杨观回 on 2016/1/11 0011.
 */
public abstract class RecycleListActivity extends ToolbarActivity {
    protected final static int EDIT = 1;
    protected final static int TIME = 2;
    protected final static int SPINNER = 3;
    protected final static int MULTI_USER = 4;
    protected final static int BUTTON = 5;
    protected final static int TEXT = 6;
    protected final static int FILE = 7;

    protected static final int HEADER_VIEW = -100;
    protected static final int FOOTER_VIEW = -200;
    protected static final int NORMAL_VIEW = 0;
    private RecyclerView recyclerView;
    protected LayoutInflater mInflater;

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = LayoutInflater.from(this);
        afterInit();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_recyclelist);
    }

    @Override
    protected void initData() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(new RecycleViewHolder());
    }


    class RecycleViewHolder extends RecyclerView.Adapter<ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return RecycleListActivity.this.onCreateViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            RecycleListActivity.this.onBindViewHolder(holder, position);
        }

        @Override
        public int getItemCount() {
            return RecycleListActivity.this.getItemCount();
        }

        @Override
        public int getItemViewType(int position) {
            return RecycleListActivity.this.getItemViewType(position);
        }
    }

    public abstract void afterInit();

    ;

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TIME:
                return new ViewHolder(new SelectTimeView(this));
            case MULTI_USER:
                return new ViewHolder(new SelectUserView(this));
            case EDIT:
                return new ViewHolder(new ImageEditView(this));
            case BUTTON:
                return new ViewHolder(mInflater.inflate(R.layout.item_button, parent, false));
            case FILE:
                return new ViewHolder(new SelectFileView(this));
        }
        return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
    }

    public abstract void onBindViewHolder(ViewHolder holder, final int position);

    public abstract int getItemViewType(int position);

    public abstract int getItemCount();


    protected Object getValue(int position) {
        int type = getItemViewType(position);
        switch (type) {
            case EDIT:
                ImageEditView editText = (ImageEditView) recyclerView.getChildAt(position);
                return editText.getValue();
            case TIME:
                SelectTimeView selectTimeView = (SelectTimeView) recyclerView.getChildAt(position);
                return selectTimeView.getValue();
            case SPINNER:
                Spinner spinner = (Spinner) recyclerView.getChildAt(position);
                return spinner.getSelectedItemId();
            case MULTI_USER:
                SelectUserView selectUserView = (SelectUserView) recyclerView.getChildAt(position);
                return selectUserView.getValue();
            case BUTTON:
                return null;
            case TEXT:
                return null;
            case FILE:
                SelectFileView selectFileView = (SelectFileView) recyclerView.getChildAt(position);
                return selectFileView.getValue();
        }
        return null;
    }
}
